/*      */ package com.chinapay.secss;
/*      */ 
/*      */ import java.io.BufferedInputStream;
/*      */ import java.io.ByteArrayInputStream;
/*      */ import java.io.File;
/*      */ import java.io.FileInputStream;
/*      */ import java.io.FileNotFoundException;
/*      */ import java.io.FileWriter;
/*      */ import java.io.IOException;
/*      */ import java.io.InputStream;
/*      */ import java.io.PrintWriter;
/*      */ import java.io.RandomAccessFile;
/*      */ import java.io.UnsupportedEncodingException;
/*      */ import java.security.InvalidKeyException;
/*      */ import java.security.MessageDigest;
/*      */ import java.security.NoSuchAlgorithmException;
/*      */ import java.security.PrivateKey;
/*      */ import java.security.PublicKey;
/*      */ import java.security.Signature;
/*      */ import java.security.SignatureException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Collections;
/*      */ import java.util.HashMap;
/*      */ import java.util.Iterator;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Properties;
/*      */ import org.apache.commons.codec.binary.Base64;
/*      */ import org.apache.log4j.Logger;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class SecssUtil
/*      */ {
/*      */   private static final char CHAR_RTN_N = '\n';
/*      */   private static final char CHAR_RTN_R = '\r';
/*      */   private static final int BUFFER_SIZE = 4096;
/*      */   private String sign;
/*      */   private String encPin;
/*      */   private String encValue;
/*      */   private String decValue;
/*      */   private String certId;
/*      */   private String errCode;
/*      */   private String errMsg;
/*      */   private boolean initFlag1 = false;
/*      */   private boolean initFlag2 = false;
/*   91 */   private long hashLength = 40960L;
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private CertUtil certUtil;
/*      */ 
/*      */ 
/*      */   
/*  100 */   private static Map<String, CertUtil> certUtilMap = new HashMap<String, CertUtil>();
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*  105 */   private static String CERTUTILMAP_DEFAULT_KEY = "DEFAULT";
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private boolean ignoreCache = false;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public SecssUtil() {
/*  117 */     this(false);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public SecssUtil(boolean ignoreCertCache) {
/*  127 */     this.ignoreCache = ignoreCertCache;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public synchronized boolean init() {
/*  136 */     if (this.initFlag1) {
/*  137 */       LogUtil.writeLog("已经从默认配置文件初始化过安全控件，不再作重复初始化工作.");
/*  138 */       return true;
/*      */     } 
/*  140 */     LogUtil.writeLog("开始从默认配置文件初始化安全控件.");
/*      */     try {
/*  142 */       this.errCode = SecssConstants.UNKNOWN_WRONG;
/*  143 */       loadCertUtil(null);
/*  144 */       this.initFlag1 = true;
/*  145 */       this.initFlag2 = false;
/*  146 */       this.errCode = SecssConstants.SUCCESS;
/*  147 */       return true;
/*  148 */     } catch (SecurityException e) {
/*  149 */       LogUtil.writeErrorLog("从默认配置文件初始化安全控件异常", e);
/*  150 */       this.errCode = e.getErrCode();
/*  151 */       this.initFlag1 = false;
/*  152 */       return false;
/*  153 */     } catch (Exception e) {
/*  154 */       LogUtil.writeErrorLog("从默认配置文件初始化安全控件异常", e);
/*  155 */       this.errCode = SecssConstants.DEFAULTINIT_GOES_WRONG;
/*  156 */       this.initFlag1 = false;
/*  157 */       return false;
/*      */     } finally {
/*  159 */       LogUtil.writeLog("从默认配置文件初始化安全控件结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public synchronized boolean init(String propPath) {
/*  171 */     LogUtil.writeLog("开始指定属性集初始化安全控件.");
/*      */     try {
/*  173 */       this.errCode = SecssConstants.UNKNOWN_WRONG;
/*  174 */       loadCertUtil(propPath);
/*  175 */       this.initFlag2 = true;
/*  176 */       this.initFlag1 = false;
/*  177 */       this.errCode = SecssConstants.SUCCESS;
/*  178 */       return true;
/*  179 */     } catch (SecurityException e) {
/*  180 */       LogUtil.writeErrorLog("从指定属性集初始化安全控件异常", e);
/*  181 */       this.errCode = e.getErrCode();
/*  182 */       this.initFlag2 = false;
/*  183 */       return false;
/*  184 */     } catch (Exception e) {
/*  185 */       LogUtil.writeErrorLog("从指定属性集初始化安全控件异常", e);
/*  186 */       this.errCode = SecssConstants.SPECIFYINIT_GOES_WRONG;
/*  187 */       this.initFlag2 = false;
/*  188 */       return false;
/*      */     } finally {
/*  190 */       LogUtil.writeLog("从指定属性集初始化安全控件结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public synchronized boolean init(Properties props) {
/*  202 */     LogUtil.writeLog("开始指定属性集初始化安全控件.");
/*      */     try {
/*  204 */       this.errCode = SecssConstants.UNKNOWN_WRONG;
/*  205 */       this.certUtil = CertUtil.init(props);
/*  206 */       this.initFlag2 = true;
/*  207 */       this.initFlag1 = false;
/*  208 */       this.errCode = SecssConstants.SUCCESS;
/*  209 */       return true;
/*  210 */     } catch (SecurityException e) {
/*  211 */       LogUtil.writeErrorLog("从指定属性集初始化安全控件异常", e);
/*  212 */       this.errCode = e.getErrCode();
/*  213 */       this.initFlag2 = false;
/*  214 */       return false;
/*  215 */     } catch (Exception e) {
/*  216 */       LogUtil.writeErrorLog("从指定属性集初始化安全控件异常", e);
/*  217 */       this.errCode = SecssConstants.SPECIFYINIT_GOES_WRONG;
/*  218 */       this.initFlag2 = false;
/*  219 */       return false;
/*      */     } finally {
/*  221 */       LogUtil.writeLog("从指定属性集初始化安全控件结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void clearCert() {
/*  229 */     certUtilMap.clear();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private void loadCertUtil(String path) throws SecurityException {
/*  241 */     String key = null;
/*  242 */     boolean isPathEmpty = !(path != null && path.trim().length() != 0);
/*  243 */     if (this.ignoreCache) {
/*  244 */       if (isPathEmpty) {
/*  245 */         this.certUtil = CertUtil.init();
/*      */       } else {
/*  247 */         Properties pros = loadProp(path);
/*  248 */         this.certUtil = CertUtil.init(pros);
/*      */       } 
/*      */     } else {
/*  251 */       if (isPathEmpty) {
/*  252 */         key = CERTUTILMAP_DEFAULT_KEY;
/*      */       } else {
/*  254 */         key = path;
/*      */       } 
/*  256 */       this.certUtil = certUtilMap.get(key);
/*  257 */       if (this.certUtil == null) {
/*  258 */         synchronized (certUtilMap) {
/*  259 */           this.certUtil = certUtilMap.get(key);
/*  260 */           if (this.certUtil == null) {
/*  261 */             if (isPathEmpty) {
/*  262 */               this.certUtil = CertUtil.init();
/*      */             } else {
/*  264 */               Properties pros = loadProp(path);
/*  265 */               this.certUtil = CertUtil.init(pros);
/*      */             } 
/*  267 */             certUtilMap.put(key, this.certUtil);
/*      */           } 
/*      */         } 
/*      */       }
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private Properties loadProp(String propPath) {
/*  282 */     Properties props = new Properties();
/*  283 */     InputStream is = null;
/*      */     try {
/*  285 */       is = new FileInputStream(propPath);
/*  286 */       props.load(is);
/*  287 */     } catch (Exception e) {
/*  288 */       LogUtil.writeErrorLog("加载配置文件失败", e);
/*  289 */       this.errCode = SecssConstants.SPECIFYINIT_GOES_WRONG;
/*  290 */       this.initFlag2 = false;
/*  291 */       return null;
/*      */     } finally {
/*  293 */       if (is != null) {
/*      */         try {
/*  295 */           is.close();
/*  296 */         } catch (IOException iOException) {}
/*      */       }
/*      */     } 
/*      */ 
/*      */     
/*  301 */     return props;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public boolean reloadSignCert(String certPath, String pass) {
/*  313 */     if (!this.initFlag1 && !this.initFlag2) {
/*  314 */       LogUtil.writeErrorLog("进行重新加载签名证书之前，请先确保已通过默认或指定属性集方式初始化过控件");
/*  315 */       return false;
/*      */     } 
/*  317 */     LogUtil.writeLog("开始重新加载签名证书.");
/*  318 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  320 */       this.certUtil.reloadSignCert(certPath, pass);
/*  321 */       return true;
/*  322 */     } catch (SecurityException e) {
/*  323 */       LogUtil.writeErrorLog("重新加载签名证书异常", e);
/*  324 */       this.errCode = e.getErrCode();
/*  325 */       return false;
/*  326 */     } catch (Exception e) {
/*  327 */       LogUtil.writeErrorLog("重新加载签名证书异常", e);
/*  328 */       this.errCode = SecssConstants.RELOADSC_GOES_WRONG;
/*  329 */       return false;
/*      */     } finally {
/*  331 */       LogUtil.writeLog("重新加载签名证书结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void sign(Map map) {
/*  342 */     sign(map, SecssConstants.SIGN512_ALGNAME);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void sign(Map map, String signAlg) {
/*  354 */     sign(map, signAlg, SecssConstants.SIGN512, this.hashLength);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected void sign(Map map, String signAlg, String hashAlg, long hashLength) {
/*  369 */     if (!this.initFlag1 && !this.initFlag2) {
/*  370 */       LogUtil.writeErrorLog("进行签名操作之前，请先通过默认或指定属性集方式初始化控件");
/*  371 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  374 */     LogUtil.writeLog("签名处理开始.");
/*  375 */     this.sign = null;
/*  376 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  378 */       String dataStr = getSignStr(map, this.certUtil.getSecssConfig()
/*  379 */           .getInvalidFileds(), true);
/*  380 */       LogUtil.writeLog(
/*  381 */           "报文签名之前的字符串(不含signature域)=[" + dataStr + 
/*  382 */           "]");
/*  383 */       byte[] dataBytes = dataStr.getBytes(SecssConstants.CHARSET_COMM);
/*      */       
/*  385 */       if (hashAlg != null && hashAlg.trim().length() > 0 && dataBytes.length > hashLength) {
/*  386 */         dataBytes = hashBytes(hashAlg, dataBytes);
/*      */       }
/*      */       
/*  389 */       PrivateKey priKey = this.certUtil.getPriKey();
/*  390 */       if (priKey == null) {
/*  391 */         throw new Exception("获取到的私钥为空");
/*      */       }
/*  393 */       byte[] signBytes = null;
/*  394 */       signBytes = SecureUtil.sign(dataBytes, priKey, signAlg);
/*  395 */       this.sign = new String(Base64.encodeBase64(signBytes), 
/*  396 */           SecssConstants.CHARSET_COMM);
/*  397 */       LogUtil.writeLog("报文签名之后的字符串=[" + 
/*  398 */           this.sign + "]");
/*  399 */       this.certId = this.certUtil.getSignCertId();
/*  400 */       LogUtil.writeLog("签名证书ID=[" + 
/*  401 */           this.certId + "]");
/*  402 */       this.errCode = SecssConstants.SUCCESS;
/*  403 */     } catch (SecurityException e) {
/*  404 */       LogUtil.writeErrorLog("签名异常", e);
/*  405 */       this.errCode = e.getErrCode();
/*      */       return;
/*  407 */     } catch (Exception e) {
/*  408 */       LogUtil.writeErrorLog("签名异常", e);
/*  409 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  412 */       LogUtil.writeLog("签名处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void signBatch(Map map) {
/*  423 */     signBatch(map, SecssConstants.SIGN512_ALGNAME);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void signBatch(Map map, String signAlg) {
/*  435 */     signBatch(map, signAlg, SecssConstants.SIGN512);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected void signBatch(Map map, String signAlg, String hashAlg) {
/*  449 */     if (!this.initFlag1 && !this.initFlag2) {
/*  450 */       LogUtil.writeErrorLog("进行签名操作之前，请先通过默认或指定属性集方式初始化控件");
/*  451 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  454 */     LogUtil.writeLog("批量签名处理开始.");
/*  455 */     this.sign = null;
/*  456 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  458 */       String dataStr = getSignStr(map, this.certUtil.getSecssConfig()
/*  459 */           .getInvalidFileds(), true);
/*  460 */       LogUtil.writeLog(
/*  461 */           "报文签名之前的字符串(不含signature域)=[" + dataStr + 
/*  462 */           "]");
/*  463 */       byte[] dataBytes = dataStr.getBytes(SecssConstants.CHARSET_COMM);
/*      */       
/*  465 */       if (hashAlg != null && hashAlg.trim().length() > 0) {
/*  466 */         dataBytes = hashBytes(hashAlg, dataBytes);
/*      */       }
/*      */       
/*  469 */       PrivateKey priKey = this.certUtil.getPriKey();
/*  470 */       if (priKey == null) {
/*  471 */         throw new Exception("获取到的私钥为空");
/*      */       }
/*  473 */       byte[] signBytes = null;
/*  474 */       signBytes = SecureUtil.sign(dataBytes, priKey, signAlg);
/*  475 */       this.sign = new String(Base64.encodeBase64(signBytes), 
/*  476 */           SecssConstants.CHARSET_COMM);
/*  477 */       LogUtil.writeLog("报文签名之后的字符串=[" + 
/*  478 */           this.sign + "]");
/*  479 */       this.certId = this.certUtil.getSignCertId();
/*  480 */       LogUtil.writeLog("签名证书ID=[" + 
/*  481 */           this.certId + "]");
/*  482 */       this.errCode = SecssConstants.SUCCESS;
/*  483 */     } catch (SecurityException e) {
/*  484 */       LogUtil.writeErrorLog("签名异常", e);
/*  485 */       this.errCode = e.getErrCode();
/*      */       return;
/*  487 */     } catch (Exception e) {
/*  488 */       LogUtil.writeErrorLog("签名异常", e);
/*  489 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  492 */       LogUtil.writeLog("签名处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void verify(Map map) {
/*  504 */     verify(map, SecssConstants.SIGN512_ALGNAME);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void verify(Map map, String verifyAlg) {
/*  516 */     verify(map, verifyAlg, SecssConstants.SIGN512, this.hashLength);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected void verify(Map map, String verifyAlg, String hashAlg, long hashLength) {
/*  531 */     if (!this.initFlag1 && !this.initFlag2) {
/*  532 */       LogUtil.writeErrorLog("进行验签操作之前，请先通过默认或指定属性集方式初始化控件");
/*  533 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  536 */     LogUtil.writeLog("验签处理开始.");
/*  537 */     boolean result = false;
/*  538 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  540 */       String signFieldName = this.certUtil.getSecssConfig().getSignFieldName();
/*  541 */       if (isEmpty(signFieldName)) {
/*  542 */         this.errCode = SecssConstants.SIGN_FIELD_NULL;
/*      */         return;
/*      */       } 
/*  545 */       String signStr = (String)map.get(signFieldName);
/*  546 */       LogUtil.writeLog("签名串=[" + signStr + 
/*  547 */           "]");
/*  548 */       if (isEmpty(signStr)) {
/*  549 */         this.errCode = SecssConstants.SIGN_VALUE_NULL;
/*      */         return;
/*      */       } 
/*  552 */       List invalidFields = this.certUtil.getSecssConfig().getInvalidFileds();
/*  553 */       String dataStr = getSignStr(map, invalidFields, true);
/*  554 */       LogUtil.writeLog(
/*  555 */           "原报文字符串(不含signature域)=[" + dataStr + 
/*  556 */           "]");
/*  557 */       byte[] dataBytes = dataStr.getBytes(SecssConstants.CHARSET_COMM);
/*      */       
/*  559 */       if (hashAlg != null && hashAlg.trim().length() > 0 && dataBytes.length > hashLength) {
/*  560 */         dataBytes = hashBytes(hashAlg, dataBytes);
/*      */       }
/*  562 */       PublicKey pubKey = this.certUtil.getPubKey();
/*  563 */       if (pubKey == null) {
/*  564 */         throw new Exception("获取到的公钥为空");
/*      */       }
/*  566 */       byte[] signBytes = Base64.decodeBase64(signStr
/*  567 */           .getBytes(SecssConstants.CHARSET_COMM));
/*  568 */       result = SecureUtil.verify(dataBytes, signBytes, pubKey, verifyAlg);
/*  569 */       if (result) {
/*  570 */         this.errCode = SecssConstants.SUCCESS;
/*      */       } else {
/*  572 */         this.errCode = SecssConstants.VERIFY_FAILED;
/*      */       } 
/*  574 */       LogUtil.writeLog(
/*  575 */           "验签结果=[" + (
/*  576 */           SecssConstants.SUCCESS.equals(this.errCode) ? 1 : 
/*  577 */           0) + "]");
/*  578 */     } catch (SecurityException e) {
/*  579 */       LogUtil.writeErrorLog("验签异常", e);
/*  580 */       this.errCode = e.getErrCode();
/*      */       return;
/*  582 */     } catch (Exception e) {
/*  583 */       LogUtil.writeErrorLog("验签异常", e);
/*  584 */       this.errCode = SecssConstants.VERIFY_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  587 */       LogUtil.writeLog("验签处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void encryptPin(String pin, String card) {
/*  600 */     if (!this.initFlag1 && !this.initFlag2) {
/*  601 */       LogUtil.writeErrorLog("进行Pin加密操作之前，请先通过默认或指定属性集方式初始化控件");
/*  602 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  605 */     LogUtil.writeLog("Pin加密处理开始.");
/*  606 */     this.encPin = null;
/*  607 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  609 */       byte[] pinBlock = SecureUtil.pin2PinBlockWithCardNO(pin, card);
/*  610 */       if (isEmpty(pinBlock)) {
/*  611 */         throw new Exception("计算得到的PinBlock为空");
/*      */       }
/*  613 */       this.encPin = encryptDataLocal(pinBlock);
/*  614 */       this.errCode = SecssConstants.SUCCESS;
/*  615 */       LogUtil.writeLog("Pin加密结果=[" + 
/*  616 */           this.encPin + "]");
/*  617 */     } catch (SecurityException e) {
/*  618 */       LogUtil.writeErrorLog("Pin加密异常", e);
/*  619 */       this.errCode = e.getErrCode();
/*      */       return;
/*  621 */     } catch (Exception e) {
/*  622 */       LogUtil.writeErrorLog("Pin加密异常", e);
/*  623 */       this.errCode = SecssConstants.ENCPIN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  626 */       LogUtil.writeLog("Pin加密处理处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void encryptData(String data) {
/*  636 */     if (!this.initFlag1 && !this.initFlag2) {
/*  637 */       LogUtil.writeErrorLog("进行加密操作之前，请先通过默认或指定属性集方式初始化控件");
/*  638 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  641 */     LogUtil.writeLog("数据加密处理开始.");
/*  642 */     this.encValue = null;
/*  643 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */ 
/*      */     
/*      */     try {
/*  647 */       this.encValue = encryptDataLocal(data
/*  648 */           .getBytes(SecssConstants.CHARSET_COMM));
/*  649 */       this.errCode = SecssConstants.SUCCESS;
/*  650 */       LogUtil.writeLog("数据加密结果=[" + 
/*  651 */           this.encValue + "]");
/*  652 */     } catch (SecurityException e) {
/*  653 */       LogUtil.writeErrorLog("数据加密异常", e);
/*  654 */       this.errCode = e.getErrCode();
/*      */       return;
/*  656 */     } catch (Exception e) {
/*  657 */       LogUtil.writeErrorLog("数据加密异常", e);
/*  658 */       this.errCode = SecssConstants.ENCDATA_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  661 */       LogUtil.writeLog("数据加密处理处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void decryptData(String data) {
/*  672 */     if (!this.initFlag1 && !this.initFlag2) {
/*  673 */       LogUtil.writeErrorLog("进行解密操作之前，请先通过默认或指定属性集方式初始化控件");
/*  674 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  677 */     LogUtil.writeLog("数据解密处理开始.");
/*  678 */     this.decValue = null;
/*  679 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  681 */       LogUtil.writeLog("解密前数据=[" + data + 
/*  682 */           "]");
/*  683 */       this.decValue = decryptDataLocal(Base64.decodeBase64(data));
/*  684 */       this.errCode = SecssConstants.SUCCESS;
/*  685 */       LogUtil.writeLog("数据解密结果=[" + 
/*  686 */           this.decValue + "]");
/*  687 */     } catch (SecurityException e) {
/*  688 */       LogUtil.writeErrorLog("数据解密异常", e);
/*  689 */       this.errCode = e.getErrCode();
/*      */       return;
/*  691 */     } catch (Exception e) {
/*  692 */       LogUtil.writeErrorLog("数据解密异常", e);
/*  693 */       this.errCode = SecssConstants.DECDATA_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  696 */       LogUtil.writeLog("数据解密处理处理结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void getSignCertId() {
/*  704 */     if (!this.initFlag1 && !this.initFlag2) {
/*  705 */       LogUtil.writeErrorLog("进行签名证书ID获取之前，请先通过默认或指定属性集方式初始化控件");
/*  706 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  709 */     LogUtil.writeLog("签名证书ID获取开始.");
/*  710 */     this.certId = null;
/*  711 */     this.errCode = SecssConstants.UNKNOWN_WRONG;
/*      */     try {
/*  713 */       this.certId = this.certUtil.getSignCertId();
/*  714 */       LogUtil.writeLog("签名证书ID=[" + 
/*  715 */           this.certId + "]");
/*  716 */       this.errCode = SecssConstants.SUCCESS;
/*  717 */     } catch (SecurityException e) {
/*  718 */       LogUtil.writeErrorLog("签名证书ID获取异常", e);
/*  719 */       this.errCode = e.getErrCode();
/*      */       return;
/*  721 */     } catch (Exception e) {
/*  722 */       LogUtil.writeErrorLog("签名证书ID获取异常", e);
/*  723 */       this.errCode = SecssConstants.GET_CERT_ID_ERROR;
/*      */       return;
/*      */     } finally {
/*  726 */       LogUtil.writeLog("签名证书ID获取结束.");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private String encryptDataLocal(byte[] dataBytes) throws SecurityException, Exception {
/*  740 */     PublicKey pubKey = this.certUtil.getPubKey();
/*  741 */     if (pubKey == null) {
/*  742 */       throw new Exception("获取到的公钥为空");
/*      */     }
/*  744 */     byte[] encBytes = 
/*  745 */       SecureUtil.encryptData(dataBytes, pubKey.getEncoded());
/*  746 */     return new String(Base64.encodeBase64(encBytes), 
/*  747 */         SecssConstants.CHARSET_COMM);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private String decryptDataLocal(byte[] dataBytes) throws SecurityException, Exception {
/*  760 */     PrivateKey priKey = this.certUtil.getPriKey();
/*  761 */     if (priKey == null) {
/*  762 */       throw new Exception("获取到的私钥为空");
/*      */     }
/*  764 */     byte[] decBytes = 
/*  765 */       SecureUtil.decryptData(dataBytes, priKey.getEncoded());
/*  766 */     return new String(decBytes, SecssConstants.CHARSET_COMM);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private String getSignStr(Map map, List invalidFields, boolean isSort) throws SecurityException {
/*  782 */     StringBuffer param = null;
/*  783 */     List<String> msgList = null;
/*  784 */     if (map == null || map.size() == 0) {
/*  785 */       throw new SecurityException(SecssConstants.GET_SIGN_STRING_ERROR);
/*      */     }
/*      */     try {
/*  788 */       param = new StringBuffer();
/*  789 */       msgList = new ArrayList();
/*  790 */       for (Iterator<String> it = map.keySet().iterator(); it.hasNext(); ) {
/*  791 */         String key = it.next();
/*  792 */         if (invalidFields != null && 
/*  793 */           invalidFields.contains(key)) {
/*      */           continue;
/*      */         }
/*      */         
/*  797 */         String value = (String)map.get(key);
/*  798 */         msgList.add(String.valueOf(key) + SecssConstants.KEY_VALUE_CONNECT + value);
/*      */       } 
/*      */       
/*  801 */       if (isSort)
/*      */       {
/*  803 */         Collections.sort(msgList);
/*      */       }
/*      */       
/*  806 */       for (int i = 0; i < msgList.size(); i++) {
/*  807 */         String msg = msgList.get(i);
/*  808 */         if (i > 0) {
/*  809 */           param.append(SecssConstants.MESSAGE_CONNECT);
/*      */         }
/*  811 */         param.append(msg);
/*      */       } 
/*      */       
/*  814 */       return param.toString();
/*  815 */     } catch (Exception e) {
/*  816 */       LogUtil.writeErrorLog("构建签名字符串异常", e);
/*  817 */       throw new SecurityException(SecssConstants.GET_SIGN_STRING_ERROR);
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public static String cpEncryptData(String src) throws Exception {
/*  831 */     CPEncryptAndDecrypt ced = new CPEncryptAndDecrypt();
/*  832 */     byte[] resultData = null;
/*      */     try {
/*  834 */       resultData = ced.encryptPassword(src
/*  835 */           .getBytes(SecssConstants.CHARSET_COMM));
/*  836 */     } catch (UnsupportedEncodingException e) {
/*  837 */       LogUtil.writeErrorLog("编码异常", e);
/*  838 */     } catch (Exception e) {
/*  839 */       LogUtil.writeErrorLog("加密异常", e);
/*      */     } 
/*  841 */     if (resultData == null) {
/*  842 */       LogUtil.writeLog("加密数据失败");
/*  843 */       return null;
/*      */     } 
/*      */     
/*  846 */     return Base64.encodeBase64String(resultData);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public static String cpDecryptData(String str) throws Exception {
/*  858 */     CPEncryptAndDecrypt ced = new CPEncryptAndDecrypt();
/*      */     
/*  860 */     byte[] resultData = null;
/*      */     try {
/*  862 */       resultData = ced.decryptPassword(Base64.decodeBase64(str));
/*  863 */     } catch (UnsupportedEncodingException e) {
/*  864 */       LogUtil.writeErrorLog("编码异常", e);
/*  865 */     } catch (Exception e) {
/*  866 */       LogUtil.writeErrorLog("解密异常", e);
/*      */     } 
/*  868 */     if (resultData == null) {
/*  869 */       LogUtil.writeLog("解密数据失败");
/*  870 */       return null;
/*      */     } 
/*  872 */     return new String(resultData, SecssConstants.CHARSET_COMM);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void signFile(String filePath) {
/*  882 */     signFile(filePath, SecssConstants.SIGN512_ALGNAME);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void signFile(String filePath, String sigAlgName) {
/*  894 */     if (!this.initFlag1 && !this.initFlag2) {
/*  895 */       LogUtil.writeErrorLog("进行签名操作之前，请先通过默认或指定属性集方式初始化控件");
/*  896 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/*  899 */     File f = new File(filePath);
/*  900 */     if (f == null || !f.exists()) {
/*  901 */       LogUtil.writeErrorLog("文件不存在");
/*  902 */       this.errCode = SecssConstants.VERIFY_FAILED;
/*      */       
/*      */       return;
/*      */     } 
/*  906 */     byte[] sha512Bytes = null;
/*  907 */     BufferedInputStream is = null;
/*      */     try {
/*  909 */       MessageDigest digest = 
/*  910 */         MessageDigest.getInstance(SecssConstants.SIGN512);
/*      */       
/*  912 */       is = new BufferedInputStream(new FileInputStream(f));
/*      */       
/*  914 */       byte[] buffer = new byte[4096];
/*      */       
/*  916 */       int readCount = is.read(buffer, 0, 4096);
/*      */       
/*  918 */       int startPos = 0;
/*  919 */       while (readCount != -1) {
/*  920 */         int i = 0;
/*  921 */         while (i < readCount) {
/*  922 */           if (buffer[i] == 13 || buffer[i] == 10) {
/*  923 */             if (i - startPos > 0) {
/*  924 */               digest.update(buffer, startPos, i - startPos);
/*      */             }
/*  926 */             startPos = i + 1;
/*      */           } 
/*  928 */           i++;
/*      */         } 
/*  930 */         if (i - startPos > 0) {
/*  931 */           digest.update(buffer, startPos, i - startPos);
/*      */         }
/*  933 */         startPos = 0;
/*  934 */         readCount = is.read(buffer, 0, 4096);
/*      */       } 
/*      */       
/*  937 */       sha512Bytes = digest.digest();
/*      */       
/*  939 */       if (Logger.getRootLogger().isDebugEnabled()) {
/*  940 */         String base64 = Base64.encodeBase64String(sha512Bytes);
/*  941 */         LogUtil.writeLog("middle sign = " + base64);
/*      */       } 
/*  943 */     } catch (FileNotFoundException e) {
/*  944 */       LogUtil.writeErrorLog("文件不存在", e);
/*  945 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*  947 */     } catch (NoSuchAlgorithmException e) {
/*  948 */       LogUtil.writeErrorLog("算法不存在", e);
/*  949 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*  951 */     } catch (UnsupportedEncodingException e) {
/*  952 */       LogUtil.writeErrorLog("不支持的编码", e);
/*  953 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*  955 */     } catch (IOException e) {
/*  956 */       LogUtil.writeErrorLog("io异常", e);
/*  957 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/*  960 */       if (is != null) {
/*      */         try {
/*  962 */           is.close();
/*  963 */         } catch (IOException iOException) {}
/*      */       }
/*      */     } 
/*      */ 
/*      */ 
/*      */     
/*  969 */     if (sha512Bytes == null) {
/*  970 */       LogUtil.writeErrorLog("签名失败");
/*  971 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       
/*      */       return;
/*      */     } 
/*  975 */     String sign = signDataBytes(sha512Bytes, sigAlgName);
/*  976 */     if (sign != null && sign.trim().length() > 0) {
/*  977 */       PrintWriter writer = null;
/*      */       
/*      */       try {
/*  980 */         writer = new PrintWriter(new FileWriter(f, true));
/*  981 */         writer.print(SecssConstants.RETURN_LINE);
/*  982 */         writer.print(sign);
/*      */         
/*  984 */         this.errCode = SecssConstants.SUCCESS;
/*  985 */       } catch (IOException e) {
/*  986 */         LogUtil.writeErrorLog("文件签名异常", e);
/*      */       } finally {
/*  988 */         if (writer != null) {
/*  989 */           writer.close();
/*      */         }
/*      */       } 
/*      */     } else {
/*  993 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private String signDataBytes(byte[] dataBytes, String sigAlgName) {
/* 1007 */     String sign = null;
/*      */     try {
/* 1009 */       PrivateKey priKey = this.certUtil.getPriKey();
/* 1010 */       if (priKey == null) {
/* 1011 */         LogUtil.writeErrorLog("获取到的公钥为空");
/* 1012 */         this.errCode = SecssConstants.SIGN_GOES_WRONG;
/* 1013 */         return null;
/*      */       } 
/* 1015 */       Signature signature = Signature.getInstance(sigAlgName);
/* 1016 */       signature.initSign(priKey);
/* 1017 */       signature.update(dataBytes);
/*      */       
/* 1019 */       sign = new String(Base64.encodeBase64(signature.sign()));
/*      */       
/* 1021 */       return sign;
/* 1022 */     } catch (InvalidKeyException e) {
/* 1023 */       LogUtil.writeErrorLog("秘钥不正确", e);
/* 1024 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/* 1025 */       return null;
/* 1026 */     } catch (NoSuchAlgorithmException e) {
/* 1027 */       LogUtil.writeErrorLog("算法不存在", e);
/* 1028 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/* 1029 */       return null;
/* 1030 */     } catch (SignatureException e) {
/* 1031 */       LogUtil.writeErrorLog("验签失败", e);
/* 1032 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/* 1033 */       return null;
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void verifyFile(String filePath) {
/* 1044 */     verifyFile(filePath, SecssConstants.SIGN512_ALGNAME);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void verifyFile(String filePath, String sigAlgName) {
/* 1056 */     if (!this.initFlag1 && !this.initFlag2) {
/* 1057 */       LogUtil.writeErrorLog("进行签名操作之前，请先通过默认或指定属性集方式初始化控件");
/* 1058 */       this.errCode = SecssConstants.NO_INIT;
/*      */       return;
/*      */     } 
/* 1061 */     File f = new File(filePath);
/* 1062 */     if (f == null || !f.exists()) {
/* 1063 */       LogUtil.writeErrorLog("文件不存在");
/* 1064 */       this.errCode = SecssConstants.VERIFY_FAILED;
/*      */       
/*      */       return;
/*      */     } 
/* 1068 */     byte[] signBytes = null;
/* 1069 */     RandomAccessFile raf = null;
/* 1070 */     int signLenth = -1;
/*      */     try {
/* 1072 */       raf = new RandomAccessFile(f, "r");
/* 1073 */       long filePos = f.length();
/* 1074 */       while (filePos >= 0L) {
/* 1075 */         raf.seek(filePos - 1L);
/* 1076 */         byte b = (byte)raf.read();
/* 1077 */         if (b == 13 || b == 10) {
/*      */           break;
/*      */         }
/* 1080 */         filePos--;
/*      */       } 
/* 1082 */       signLenth = (int)(raf.length() - filePos);
/* 1083 */       byte[] temp = new byte[signLenth];
/* 1084 */       raf.read(temp, 0, signLenth);
/* 1085 */       if (Logger.getRootLogger().isDebugEnabled()) {
/* 1086 */         LogUtil.writeLog("sign = " + new String(temp));
/*      */       }
/* 1088 */       signBytes = Base64.decodeBase64(temp);
/* 1089 */     } catch (FileNotFoundException e) {
/* 1090 */       LogUtil.writeErrorLog("文件不存在", e);
/* 1091 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/* 1093 */     } catch (IOException e) {
/* 1094 */       LogUtil.writeErrorLog("读取文件失败", e);
/* 1095 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/* 1098 */       if (raf != null) {
/*      */         try {
/* 1100 */           raf.close();
/* 1101 */         } catch (IOException iOException) {}
/*      */       }
/*      */     } 
/*      */ 
/*      */     
/* 1106 */     if (signBytes == null) {
/* 1107 */       LogUtil.writeErrorLog("获取签名失败");
/* 1108 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       
/*      */       return;
/*      */     } 
/* 1112 */     byte[] sha512Bytes = null;
/* 1113 */     long leftLenth = f.length();
/* 1114 */     BufferedInputStream is = null;
/*      */     try {
/* 1116 */       MessageDigest digest = 
/* 1117 */         MessageDigest.getInstance(SecssConstants.SIGN512);
/*      */       
/* 1119 */       is = new BufferedInputStream(new FileInputStream(f));
/*      */       
/* 1121 */       byte[] buffer = new byte[4096];
/* 1122 */       int readCount = is.read(buffer, 0, 4096);
/*      */       
/* 1124 */       int startPos = 0;
/* 1125 */       while (readCount != -1) {
/* 1126 */         int i = 0;
/* 1127 */         while (i < readCount && leftLenth > signLenth) {
/* 1128 */           if (buffer[i] == 13 || buffer[i] == 10) {
/* 1129 */             if (i - startPos > 0) {
/* 1130 */               digest.update(buffer, startPos, i - startPos);
/*      */             }
/* 1132 */             startPos = i + 1;
/*      */           } 
/* 1134 */           i++;
/* 1135 */           leftLenth--;
/*      */         } 
/* 1137 */         if (i - startPos > 0) {
/* 1138 */           digest.update(buffer, startPos, i - startPos);
/*      */         }
/* 1140 */         if (leftLenth <= signLenth) {
/*      */           break;
/*      */         }
/* 1143 */         startPos = 0;
/* 1144 */         readCount = is.read(buffer, 0, 4096);
/*      */       } 
/*      */       
/* 1147 */       sha512Bytes = digest.digest();
/*      */       
/* 1149 */       if (Logger.getRootLogger().isDebugEnabled()) {
/* 1150 */         String base64 = Base64.encodeBase64String(sha512Bytes);
/* 1151 */         LogUtil.writeLog("middle sign = " + base64);
/*      */       } 
/* 1153 */     } catch (FileNotFoundException e) {
/* 1154 */       LogUtil.writeErrorLog("文件不存在", e);
/* 1155 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/* 1157 */     } catch (NoSuchAlgorithmException e) {
/* 1158 */       LogUtil.writeErrorLog("算法不存在", e);
/* 1159 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/* 1161 */     } catch (UnsupportedEncodingException e) {
/* 1162 */       LogUtil.writeErrorLog("不支持的编码", e);
/* 1163 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/* 1165 */     } catch (IOException e) {
/* 1166 */       LogUtil.writeErrorLog("io异常", e);
/* 1167 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       return;
/*      */     } finally {
/* 1170 */       if (is != null) {
/*      */         try {
/* 1172 */           is.close();
/* 1173 */         } catch (IOException iOException) {}
/*      */       }
/*      */     } 
/*      */ 
/*      */ 
/*      */     
/* 1179 */     if (sha512Bytes == null) {
/* 1180 */       LogUtil.writeErrorLog("签名失败");
/* 1181 */       this.errCode = SecssConstants.SIGN_GOES_WRONG;
/*      */       
/*      */       return;
/*      */     } 
/* 1185 */     boolean result = verifyFile(sha512Bytes, signBytes, sigAlgName);
/* 1186 */     if (result) {
/* 1187 */       this.errCode = SecssConstants.SUCCESS;
/*      */     } else {
/* 1189 */       this.errCode = SecssConstants.VERIFY_FAILED;
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private boolean verifyFile(byte[] dataBytes, byte[] signBytes, String sigAlgName) {
/* 1208 */     boolean isSucess = false;
/*      */     try {
/* 1210 */       PublicKey pubKey = this.certUtil.getPubKey();
/* 1211 */       if (pubKey == null) {
/* 1212 */         LogUtil.writeErrorLog("获取到的公钥为空");
/* 1213 */         this.errCode = SecssConstants.VERIFY_FAILED;
/* 1214 */         return false;
/*      */       } 
/* 1216 */       Signature signature = Signature.getInstance(sigAlgName);
/* 1217 */       signature.initVerify(pubKey);
/*      */       
/* 1219 */       signature.update(dataBytes);
/* 1220 */       isSucess = signature.verify(signBytes);
/*      */     }
/* 1222 */     catch (InvalidKeyException e) {
/* 1223 */       LogUtil.writeErrorLog("秘钥不正确", e);
/* 1224 */       this.errCode = SecssConstants.VERIFY_FAILED;
/* 1225 */       isSucess = false;
/* 1226 */     } catch (NoSuchAlgorithmException e) {
/* 1227 */       LogUtil.writeErrorLog("算法不存在", e);
/* 1228 */       this.errCode = SecssConstants.VERIFY_FAILED;
/* 1229 */       isSucess = false;
/* 1230 */     } catch (SignatureException e) {
/* 1231 */       LogUtil.writeErrorLog("验签失败", e);
/* 1232 */       this.errCode = SecssConstants.VERIFY_FAILED;
/* 1233 */       isSucess = false;
/*      */     } 
/*      */     
/* 1236 */     return isSucess;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private byte[] hashBytes(String hashAlg, byte[] dataBytes) throws NoSuchAlgorithmException {
/* 1252 */     MessageDigest digest = MessageDigest.getInstance(hashAlg.trim());
/*      */     
/* 1254 */     ByteArrayInputStream is = null;
/*      */     
/*      */     try {
/* 1257 */       is = new ByteArrayInputStream(dataBytes);
/* 1258 */       byte[] buffer = new byte[4096];
/*      */       
/* 1260 */       int readCount = is.read(buffer, 0, 4096);
/*      */       
/* 1262 */       while (readCount != -1) {
/* 1263 */         digest.update(buffer, 0, readCount);
/* 1264 */         readCount = is.read(buffer, 0, 4096);
/*      */       } 
/*      */       
/* 1267 */       dataBytes = digest.digest();
/*      */       
/* 1269 */       String base64 = Base64.encodeBase64String(dataBytes);
/* 1270 */       LogUtil.writeLog("hash = " + base64);
/*      */       
/* 1272 */       return dataBytes;
/*      */     } finally {
/* 1274 */       if (is != null) {
/*      */         try {
/* 1276 */           is.close();
/* 1277 */         } catch (IOException iOException) {}
/*      */       }
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public static boolean isEmpty(String s) {
/* 1292 */     return !(s != null && !"".equals(s.trim()));
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public static boolean isEmpty(byte[] b) {
/* 1302 */     return !(b != null && b.length >= 1);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getSign() {
/* 1310 */     return this.sign;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getEncPin() {
/* 1318 */     return this.encPin;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getEncValue() {
/* 1326 */     return this.encValue;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getDecValue() {
/* 1334 */     return this.decValue;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getCertId() {
/* 1342 */     return this.certId;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getErrCode() {
/* 1350 */     return this.errCode;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getErrMsg() {
/* 1358 */     if (isEmpty(this.errCode)) {
/* 1359 */       this.errMsg = (String)SecssConstants.ERRMAP
/* 1360 */         .get(SecssConstants.UNKNOWN_WRONG);
/*      */     } else {
/* 1362 */       this.errMsg = (String)SecssConstants.ERRMAP.get(this.errCode);
/*      */     } 
/* 1364 */     if (isEmpty(this.errMsg)) {
/* 1365 */       this.errMsg = (String)SecssConstants.ERRMAP
/* 1366 */         .get(SecssConstants.UNKNOWN_WRONG);
/*      */     }
/* 1368 */     return this.errMsg;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public long getHashLength() {
/* 1377 */     return this.hashLength;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public void setHashLength(long hashLength) {
/* 1385 */     this.hashLength = hashLength;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public static void main(String[] args) throws Exception {
/* 1395 */     String str = "你好";
/* 1396 */     String enStr = cpEncryptData(str);
/* 1397 */     System.out.println("加密后的数据：" + enStr);
/* 1398 */     String decStr = cpDecryptData(enStr);
/* 1399 */     System.out.println("解密后的数据:" + decStr);
/*      */   }
/*      */ }


/* Location:              C:\Users\Administrator\Downloads\jd-gui-windows-1.6.6\jd-gui-windows-1.6.6\chinapay-secure-1.5.1.jar!\com\chinapay\secss\SecssUtil.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */